package cn.stylefeng.guns.modular.wms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.rms.entity.Company;
import cn.stylefeng.guns.modular.rms.entity.CompanyPosition;
import cn.stylefeng.guns.modular.rms.mapper.CompanyMapper;
import cn.stylefeng.guns.modular.rms.mapper.CompanyPositionMapper;
import cn.stylefeng.guns.modular.rms.model.params.CompanyPositionParam;
import cn.stylefeng.guns.modular.rms.model.result.CompanyPositionResult;
import cn.stylefeng.guns.modular.ums.entity.MemberAccount;
import cn.stylefeng.guns.modular.ums.entity.MemberAccountRecords;
import cn.stylefeng.guns.modular.ums.mapper.MemberAccountMapper;
import cn.stylefeng.guns.modular.ums.mapper.MemberAccountRecordsMapper;
import cn.stylefeng.guns.modular.ums.mapper.MemberMapper;
import cn.stylefeng.guns.modular.ums.model.params.MemberAccountParam;
import cn.stylefeng.guns.modular.ums.model.params.MemberAccountRecordsParam;
import cn.stylefeng.guns.modular.ums.model.params.MemberParam;
import cn.stylefeng.guns.modular.ums.model.result.MemberAccountResult;
import cn.stylefeng.guns.modular.ums.model.result.MemberResult;
import cn.stylefeng.guns.modular.ums.service.MemberAccountRecordsService;
import cn.stylefeng.guns.modular.ums.service.MemberAccountService;
import cn.stylefeng.guns.modular.wms.entity.ApplyLog;
import cn.stylefeng.guns.modular.wms.entity.WorkExp;
import cn.stylefeng.guns.modular.wms.mapper.*;
import cn.stylefeng.guns.modular.wms.model.params.*;
import cn.stylefeng.guns.modular.wms.model.result.ApplyLogResult;
import cn.stylefeng.guns.modular.wms.model.result.ApplyReadResult;
import cn.stylefeng.guns.modular.wms.model.result.JobExpectResult;
import cn.stylefeng.guns.modular.wms.model.result.WorkExpResult;
import  cn.stylefeng.guns.modular.wms.service.ApplyLogService;
import cn.stylefeng.guns.utils.StringUtil;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 应聘记录表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2021-03-23
 */
@Service
public class ApplyLogServiceImpl extends ServiceImpl<ApplyLogMapper, ApplyLog> implements ApplyLogService {

    @Resource
    private CompanyPositionMapper companyPositionMapper;
    @Resource
    private ResumeMapper resumeMapper;
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private WorkExpMapper workExpMapper;
    @Resource
    private ApplyReadMapper applyReadMapper;
    @Resource
    private JobExpectMapper jobExpectMapper;
    @Resource
    private MemberAccountService accountService;
    @Resource
    private MemberAccountRecordsService accountRecordsService;

    @Override
    public void add(ApplyLogParam param){
        ApplyLog entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ApplyLogParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ApplyLogParam param){
        ApplyLogResult result=this.findBySpec(param);
        if (result!=null){
            param.setId(result.getId());
            ApplyLog oldEntity = getOldEntity(param);
            ApplyLog newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            this.updateById(newEntity);
        }else{
            this.add(param);
        }
    }

    @Transactional
    @Override
    public void updateApplyStatus(ApplyLogParam param) {
        ApplyLog oldEntity = getOldEntity(param);
        ApplyLog newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
        if (param.getStatus()==2){
            //录用，查询是否分享，如果是分享的，则给分享者加入一笔佣金
           ApplyLogResult result= this.findBySpec(new ApplyLogParam().setId(param.getId()));
           if (StringUtil.isNotEmpty(result.getTcode())){
               //查询佣金金额
               CompanyPositionResult companyPosition= companyPositionMapper.findBySpec(new CompanyPositionParam().setId(result.getPositionId()));
               if (companyPosition.getShareMoney().compareTo(BigDecimal.ZERO)==0){//岗位无佣金
                   return;
               }
               //根据tcode获取用户信息
               MemberResult memberResult=memberMapper.getMember(new MemberParam().setMemberSn(result.getTcode()));
               //更新账户
              MemberAccount memberAccount=accountService.findByMemberId(memberResult.getMemberId());
              if(memberAccount==null){
                  //创建会员账户信息
                  MemberAccountParam accountParam=new MemberAccountParam();
                  accountParam.setMemberId(memberResult.getMemberId());
                  accountParam.setBalance(companyPosition.getShareMoney());
                  accountParam.setBalanceWithdraw(companyPosition.getShareMoney());
                  accountService.add(accountParam);
              }else{
                  memberAccount.setBalance(memberAccount.getBalance().add(companyPosition.getShareMoney()));
                  memberAccount.setBalanceWithdraw(memberAccount.getBalanceWithdraw().add(companyPosition.getShareMoney()));
                  accountService.updateById(memberAccount);
              }
               //添加收入记录
               MemberAccountRecordsParam accountRecordsParam=new MemberAccountRecordsParam();
               accountRecordsParam.setMemberId(memberResult.getMemberId());
               accountRecordsParam.setMoney(companyPosition.getShareMoney());
               accountRecordsParam.setSign(1);
               accountRecordsParam.setDataId(param.getId());
               accountRecordsParam.setType(1);
               accountRecordsParam.setContent("分享职位佣金收入");
               accountRecordsService.add(accountRecordsParam);
           }
        }
    }

    @Override
    public ApplyLogResult findBySpec(ApplyLogParam param){
        return baseMapper.findBySpec(param);
    }

    @Override
    public List<ApplyLogResult> findListBySpec(ApplyLogParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ApplyLogParam param){
        Page pageContext = getPageContext();
        IPage<ApplyLogResult> page = this.baseMapper.customPageList(pageContext, param);
        if(param.getType()==0){
            page.getRecords().forEach(item -> {
                if (item.getPositionId()!=null){
                    item.setPosition(companyPositionMapper.findBySpec(new CompanyPositionParam().setId(item.getPositionId())));
                    //招聘者信息
                    MemberResult memberResult=memberMapper.getMemberById(item.getComMemberId());
                    item.setMemberAvatar(memberResult.getAvatar());
                    item.setMemberName(memberResult.getName());
                    item.setMemberPostName(memberResult.getPostName());
                    //企业信息
                    Company company=companyMapper.selectById(item.getCompanyId());
                    item.setCompanyName(company.getName());
                    item.setCompanyStaffSize(company.getStaffSize());
                    item.setCompanyNature(company.getNature());
                }
            });
        }else{
            page.getRecords().forEach(item -> {
                item.setResume(resumeMapper.findBySpec(new ResumeParam().setMemberId(item.getMemberId())));
                //求职者信息
                MemberResult memberResult=memberMapper.getMemberById(item.getMemberId());
                item.setMemberAvatar(memberResult.getAvatar());
                item.setMemberName(memberResult.getName());
                item.setMemberPostName(memberResult.getPostName());
                item.setMemberSex(memberResult.getSex());
                //工作经历
                List<WorkExpResult> list=workExpMapper.customList(new WorkExpParam().setMemberId(item.getMemberId()));
                if (list.size()>0){
                    item.setCompanyName(list.get(0).getCompanyName());
                    item.setCompanyPostName(list.get(0).getPostName());
                }
                //期望薪资
                List<JobExpectResult> expectResults =jobExpectMapper.customList(new JobExpectParam().setId(item.getMemberId()));
                if (expectResults.size()>0){
                    item.setExpectSalary(expectResults.get(0).getExpectSalary());
                }
                //应聘岗位名称
                CompanyPositionResult companyPositionResult =companyPositionMapper.findBySpec(new CompanyPositionParam().setId(item.getPositionId()));
                if (companyPositionResult!=null){
                    item.setPostName(companyPositionResult.getPostName());
                }
                //是否已读
                ApplyReadResult readResult =applyReadMapper.findBySpec(new ApplyReadParam().setApplyId(item.getId()));
                if (readResult!=null){
                    item.setIfRead(true);
                }
            });
        }
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpecForAdmin(ApplyLogParam param) {
        Page pageContext = getPageContext();
        IPage<ApplyLogResult> page =this.baseMapper.customPageListForAdmin(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ApplyLogParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ApplyLog getOldEntity(ApplyLogParam param) {
        return this.getById(getKey(param));
    }

    private ApplyLog getEntity(ApplyLogParam param) {
        ApplyLog entity = new ApplyLog();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
